import data from '@/area.js'
import utils from '@/utils'
import pca from "@/pca-code"
import newPca from "@/pca-code-new"

const province = data.province
const city = data.city
const district = data.district

const list = [province, city, district]

export function newArea() {
    return pca
}
export function newArea3() {
    let areaList = [];
    pca.forEach(el => {
        let areaData = {};
        let replaceStr = /省|自治区|市|特别行政区|回族|维吾尔/g;
        areaData.name = el.name.replace(replaceStr, '');
        areaData.children = el.children;
        areaList.push(areaData)
    });
    console.log(1111, areaList)
    return areaList;
}
export function newArea2(type=1) {
    let areaList = [];
    newPca.forEach(el => {
        let areaData = {};
        areaData.name = el.name;
        areaData.code = el.code;
        areaData.children = el.children;
        areaData.children.filter(item=>{
            switch (el.name) {
                case '北京市':
                    type==1?item.name = '北京市':item.name = '市辖区';
                    break;
                case '上海市':
                    type==1?item.name = '上海市':item.name = '市辖区';
                    break;
                case '天津市':
                    type==1?item.name = '天津市':item.name = '市辖区';
                    break;
                case '重庆市':
                    item.name = item.name == '市辖区'?'重庆市':'县';
                    break;
            }
            if(item.children){
                delete item.children;
            }
        });
        areaList.push(areaData)
    });
    return areaList;
}
//
export function newArea1() {
    let areaList = [];
    pca.forEach(el => {
        let areaData = {};
        let replaceStr = /省|自治区|市|特别行政区|回族|维吾尔/g;
        areaData.label = el.name.replace(replaceStr, '');
        areaData.value = el.name.replace(replaceStr, '');
        areaData.children = el.children;
        delete areaData.children;
        areaList.push(areaData)
    });
    return areaList;
}
function getChildren(pCode, child) {
    return child.filter(item => item.parent_code === pCode)
}

export default function (level = 3) {
    if (![1, 2, 3].includes(Number(level))) return []
    // 反转是因为，级别小的必须排前面。。
    const _list = utils.deepClone([...list].splice(0, level).reverse())
    for (let i = 0; i < _list.length - 1; i++) {
        _list[i + 1].forEach(item => {
            item.children = getChildren(item.area_code, _list[i])
        })
    }
    return _list[_list.length - 1]
}
export function getArea2(level = 3) {
    if (![1, 2, 3].includes(Number(level))) return []
    // 反转是因为，级别小的必须排前面。。
    const _list = utils.deepClone([...list].splice(0, level).reverse())
    for (let i = 0; i < _list.length - 1; i++) {
        _list[i + 1].forEach(item => {
            item.children = getChildren(item.area_code, _list[i])
        })
    }
    return setArea(_list[_list.length - 1])
}

function setArea(arr) {
    const res = []
    arr.forEach(item => {
        let cache = {
            value: item.area_code,
            label: item.area_name,
            children: [],
        };
        if (item.children) {
            cache.children = setArea(item.children)
        } else {
            delete cache.children
        }
        res.push(cache)
    })
    return res
}
export function getProvinceCityArea (level = 3) {
    if (![1, 2, 3].includes(Number(level))) return []
    // 反转是因为，级别小的必须排前面。。
    const _list = utils.deepClone([...list].splice(0, level).reverse())
    for (let i = 0; i < _list.length - 1; i++) {
        _list[i + 1].forEach(item => {
            item.children = getChildren(item.area_code, _list[i])
        })
    }
    return _list[_list.length - 1]
}
